package com.example.idachuappone;

import java.util.Calendar;

import org.json.JSONObject;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.idachuappone.cook.CookFragTwo;
import com.example.idachuappone.index.IndexFragTwo;
import com.example.idachuappone.index.welcomentity.Welcome;
import com.example.idachuappone.order.OrderFragTwo;
import com.example.idachuappone.person.PersonFragTwo;
import com.example.idachuappone.person.item.LoginActivity;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.CheckVersionUpdate;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class MainTWo extends BaseActivity implements OnCheckedChangeListener {
	private CheckVersionUpdate checkVersionUpdate;
	private CookFragTwo cookFragTwo;
	private IndexFragTwo indexFragTwo;
	private boolean isgoorder;
	private long lastBackClickTime;
	private int loginRequest;
	private RadioButton now_btn;
	private OrderFragTwo orderFragTwo;
	private PersonFragTwo personFragTwo;
	private RadioButton rbtn_cook;
	private RadioButton rbtn_index;
	private RadioButton rbtn_order;
	private RadioButton rbtn_person;
	private RadioGroup rg_main;
	private Runnable runnable;

	public MainTWo() {
		this.loginRequest = 255;
		this.runnable = new Runnable() {
			@Override
			public void run() {
				MainTWo.this.isCheckVersion();
			}
		};
		this.lastBackClickTime = 0;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_two);
		this.checkVersionUpdate = new CheckVersionUpdate();
		initView();
		showIndex();
		this.now_btn = this.rbtn_index;
		if (Calendar.getInstance().get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY) != PrefUtil.getInstance(this)
				.getMainVersion()) {
			new Handler().postDelayed(this.runnable, 3000);
			PrefUtil.getInstance(this).setMainVersion(0);
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		this.isgoorder = intent.getBooleanExtra("isgoorder", false);
		if (this.isgoorder) {
			AppShareData.isRefreshOrderAll = true;
			AppShareData.isRefreshOrderPay = true;
			AppShareData.isRefreshOrderComment = true;
			this.rbtn_order.performClick();
		}
	}

	private void isCheckVersion() {
		if (LoadingActivity.welcome.getVersion() == null) {
			NetUtil.get(this, Constant.PACKAGES, 100, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					try {
						JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
						Welcome welcome = new Welcome();
						welcome.parseJson(jsonObject);
						LoadingActivity.welcome = welcome;
						MainTWo.this.isCheckVersion();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
				}
			});
		} else if (this.checkVersionUpdate.isYesUpdate(this, LoadingActivity.welcome.getVersion())) {
			showMinDialog();
		} else if (this.checkVersionUpdate.isUpdate(this, LoadingActivity.welcome.getVersion())) {
			showUpdateDialog();
		}
	}

	private void showMinDialog() {
		new Builder(this).setTitle("\u7248\u672c\u66f4\u65b0")
				.setMessage("\u5f53\u524d\u7248\u672c\u5fc5\u987b\u66f4\u65b0")
				.setPositiveButton("\u786e\u5b9a", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						MainTWo.this.startActivity(new Intent("android.intent.action.VIEW", Uri
								.parse(LoadingActivity.welcome.getVersion().getAndroid())));
					}
				}).setCancelable(false).show();
	}

	private void showUpdateDialog() {
		String content = "";
		for (int i = 0; i < LoadingActivity.welcome.getVersion().getContent().size(); i++) {
			content = new StringBuilder(String.valueOf(content)).append(i + 1).append(".")
					.append(LoadingActivity.welcome.getVersion().getContent().get(i)).append("\n").toString();
		}
		Dialog dialog = new Builder(this).setTitle("\u7248\u672c\u66f4\u65b0").setMessage(content)
				.setPositiveButton("\u7acb\u5373\u5b89\u88c5", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						MainTWo.this.startActivity(new Intent("android.intent.action.VIEW", Uri
								.parse(LoadingActivity.welcome.getVersion().getAndroid())));
					}
				}).setNegativeButton("\u4ee5\u540e\u518d\u8bf4", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						PrefUtil.getInstance(MainTWo.this).setMainVersion(
								Calendar.getInstance().get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY));
					}
				}).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		this.rg_main = (RadioGroup) findViewById(R.id.rg_main);
		this.rg_main.setOnCheckedChangeListener(this);
		this.rbtn_order = (RadioButton) findViewById(R.id.rbtn_order);
		this.rbtn_index = (RadioButton) findViewById(R.id.rbtn_index);
		this.rbtn_cook = (RadioButton) findViewById(R.id.rbtn_cook);
		this.rbtn_person = (RadioButton) findViewById(R.id.rbtn_person);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rbtn_index /* 2131165329 */:
			showIndex();
			this.now_btn = this.rbtn_index;
		case R.id.rbtn_cook /* 2131165330 */:
			showCook();
			this.now_btn = this.rbtn_cook;
		case R.id.rbtn_order /* 2131165331 */:
			if (PrefUtil.getInstance(this).getUID() == null) {
				startActivityForResult(new Intent(this, LoginActivity.class), this.loginRequest);
			} else {
				showOrder();
			}
		case R.id.rbtn_person /* 2131165332 */:
			showPerson();
			this.now_btn = this.rbtn_person;
		default:
		}
	}

	private void showIndex() {
		hideFrag();
		this.ft = this.fm.beginTransaction();
		if (this.indexFragTwo == null) {
			this.indexFragTwo = new IndexFragTwo();
			this.ft.add(R.id.fl_content_main_two, this.indexFragTwo, "index");
		}
		this.ft.show(this.indexFragTwo);
		this.ft.commitAllowingStateLoss();
	}

	private void showCook() {
		hideFrag();
		this.ft = this.fm.beginTransaction();
		if (this.cookFragTwo == null) {
			this.cookFragTwo = new CookFragTwo();
			this.ft.add(R.id.fl_content_main_two, this.cookFragTwo, "cook");
		}
		this.ft.show(this.cookFragTwo);
		this.ft.commitAllowingStateLoss();
	}

	private void showOrder() {
		hideFrag();
		this.ft = this.fm.beginTransaction();
		if (this.orderFragTwo == null) {
			this.orderFragTwo = new OrderFragTwo();
			this.ft.add(R.id.fl_content_main_two, this.orderFragTwo, "order");
		}
		this.ft.show(this.orderFragTwo);
		this.ft.commitAllowingStateLoss();
	}

	private void showPerson() {
		hideFrag();
		this.ft = this.fm.beginTransaction();
		if (this.personFragTwo == null) {
			this.personFragTwo = new PersonFragTwo();
			this.ft.add(R.id.fl_content_main_two, this.personFragTwo, "person");
		}
		this.ft.show(this.personFragTwo);
		this.ft.commitAllowingStateLoss();
	}

	private void hideFrag() {
		this.indexFragTwo = (IndexFragTwo) this.fm.findFragmentByTag("index");
		this.cookFragTwo = (CookFragTwo) this.fm.findFragmentByTag("cook");
		this.orderFragTwo = (OrderFragTwo) this.fm.findFragmentByTag("order");
		this.personFragTwo = (PersonFragTwo) this.fm.findFragmentByTag("person");
		this.ft = this.fm.beginTransaction();
		if (this.indexFragTwo != null) {
			this.ft.hide(this.indexFragTwo);
		}
		if (this.cookFragTwo != null) {
			this.ft.hide(this.cookFragTwo);
		}
		if (this.orderFragTwo != null) {
			this.ft.hide(this.orderFragTwo);
		}
		if (this.personFragTwo != null) {
			this.ft.hide(this.personFragTwo);
		}
		this.ft.commitAllowingStateLoss();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == 4) {
			if (System.currentTimeMillis() - this.lastBackClickTime > 2000) {
				MainToast.show(this, "\u518d\u6309\u4e00\u6b21\u9000\u51fa\u7231\u5927\u53a8", 0);
				this.lastBackClickTime = System.currentTimeMillis();
			} else {
				exitApp(this);
				AppShareData.isPersonFrag = true;
				System.exit(0);
			}
		}
		return true;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode != this.loginRequest) {
			return;
		}
		if (resultCode != -1) {
			this.now_btn.performClick();
		} else if (PrefUtil.getInstance(this).getUID() != null) {
			this.now_btn.performClick();
			this.rbtn_order.performClick();
		}
	}
}